/**
 * Created by sky_wang on 2017/4/14.
 */
REPORT_VIEW.REPORT_DESIGNED = {};
var REPORT_MAKE = REPORT_VIEW.REPORT_DESIGNED;

REPORT_MAKE.data = [];
REPORT_MAKE.reportData =[];
REPORT_MAKE.view = new Object();
REPORT_MAKE.queryAllObj = [];
REPORT_MAKE.options= {
    columnDefs             : [],
    rowData                : [],
    enableSorting: true,
    rowHeight: 40,
    colWidth: 100,
    showToolPanel:false,
    suppressColumnVirtualisation:true,
    toolPanelSuppressRowGroups:true,
    toolPanelSuppressValues: true,
    toolPanelSuppressPivots: true,
    toolPanelSuppressPivotMode: true

};
REPORT_MAKE.refreshData = function (){
    $(".make_show_select_view_options").empty();
    $(".make_show_select_view_options").append("<option value='default'>---请选择---</option>");
    $.get("view/list", function(data) {
        REPORT_MAKE.data = data;
        console.log(data);
        for (var i =0; i < data.length; i++)
        {
            if (null == data[i]["view_name"])
            {
                continue;
            }
            $(".make_show_select_view_options").append("<option value='"+data[i]["vid"]+"'>"+data[i]["view_name"]+"</option>");
        }
    });
}
$(document).ready(function() {
    REPORT_MAKE.refreshData();
    $('.make_show_content').hide();

    var gridDiv = document.querySelector('.make_show_grid');
    new agGrid.Grid(gridDiv, REPORT_MAKE.options);
    $(".make_show_select_view_options").change(function(){
        REPORT_MAKE.reportData = [];
        REPORT_MAKE.view = new Object();
        $('.make_show_content').hide();
    });
    $(".make_show_confirm_view").click(function () {
        if ($(".make_show_select_view_options").val() == 'default')
        {
            alert("请选择模版")
            return;
        }
        REPORT_MAKE.data.map(function (ele){
            if ($(".make_show_select_view_options").val() == ele["vid"]) {
                REPORT_MAKE.view = ele;
                console.log("REPORT_MAKE.view:" + REPORT_MAKE.view);
                if ("" != REPORT_MAKE.view["query_ids"]) {
                    REPORT_MAKE.queryAllObj = JSON.parse(REPORT_MAKE.view["query_ids"]);
                }
                $(".make_show_query_options").html( REPORT_MAKE.view["query_html"]);
                REPORT_MAKE.options.api.setColumnDefs(JSON.parse(REPORT_MAKE.view["cols_of_sub_groups"]));
                REPORT_MAKE.options.api.softRefreshView();
            }
        });

        $('.make_show_content').show();
    });

    $(".make_show_search").click(function() {
        var queryOpt = [];
        for (var i =0; i < REPORT_MAKE.queryAllObj.length; i++)
        {
            var query_sub_opt = new Object();
            var query_sub_opt_value = new Object();
            var query_id = "#" +  REPORT_MAKE.queryAllObj[i]["key"];
            query_sub_opt_value = REPORT_MAKE.queryAllObj[i]["value"];
            if (null == $(query_id).val() || '' == $(query_id).val() || 'default' == $(query_id).val())
            {
                continue;
            }
            query_sub_opt["key"]= REPORT_MAKE.queryAllObj[i]["key"];
            query_sub_opt_value["value"] = $(query_id).val();
            query_sub_opt["value"] = query_sub_opt_value;
            queryOpt.push(query_sub_opt);
        }
        $.get("view/"+ REPORT_MAKE.view["vid"]+"/query_data", {query_data:JSON.stringify(queryOpt)},function(data){
            REPORT_MAKE.reportData = data;
            REPORT_MAKE.options.api.setRowData(data);
        });
    });


    $(".make_show_save_data").click(function() {
        if (REPORT_MAKE.reportData.length ==0)
        {
            alert("报表数据不能为空");
            return;
        }
        var info = "请输入报表名称";
        var report_name = prompt(info);
        if (report_name == ''){
            alert("报表名称不能为空");
            return;
        }

        var data = new Object();
        data["report_name"] = report_name;
        data["report_data"] = (JSON.stringify(REPORT_MAKE.reportData));
        data["view_for_grid_id"] = REPORT_MAKE.view["vid"];
        $.ajax({
            type : "post",
            url:  "report/new",
            data: {data:JSON.stringify(data)},
            async : true,
            success : function(data){
                alert("保存成功");
            }
        });
    });
    $(".make_show_excel").click(function() {
        if (""== $(".make_show_excel_file_name").val())
        {
            alert("请输入文件名");
            return;
        }
        REPORT_MAKE.options.api.exportDataAsExcel({
            skipHeader: false,
            columnGroups: false,
            skipFooters: false,
            skipGroups: false,
            skipFloatingTop: false,
            skipFloatingBottom: false,
            allColumns: false,
            onlySelected: false,
            fileName: $(".make_show_excel_file_name").val()
        });
    });
});